# name : Response summary and comparison (Macro)
# key : rsp.compare.macro
# contributor: Shuguang Sun
# --
** %let refgroup = "Arm4";
** %let trtgroup = "Arm1;Arm2;Arm3";
** options mprint mlogic;

%macro rsp_sum (
  dtin = ,
  dtout = ,
  rsp = ,
  group = ,
  strata = ,
  bygroup = ,
  freqfmt = ,
  transfmt = ,
  markref = -,
  )/store;

  proc sort data=&dtin.;
    by &bygroup. &group. &rsp.;
  run;

  ** all combinations, to avoid zero cell;
  proc sql;
    create table _rspallcomp as
      select DISTINCT
      %if &bygroup. ne %then %do;
      a.&bygroup.,
        %end;
      a.&group., b.&rsp.
        from &dtin. (KEEP = &bygroup. &group.) as a, &dtin. (KEEP = &rsp.) as b
        order BY %if &bygroup. ne %then %do;
        &bygroup.,
          %end;
        &group., &rsp.;
        quit;
  run;

  data &dtin.;
    merge &dtin. (IN=a) _rspallcomp (IN=b);
    by &bygroup. &group. &rsp.;
    IF b AND not a THEN _wtg = 0;
    ELSE _wtg = 1;
    ** IF _wtg EQ 0 THEN AVISITN = _AVISITN;
  run;

  ods SELECT none;

  PROC FREQ data = &dtin. %if &freqfmt. NE %then %do;
    order = formatted
      %end;;
    %if &freqfmt. NE %then %do;
      FORMAT &group. &freqfmt.;
      %end;
    TABLE &rsp. / binomial(level = '1' exact)  out = _rowpct;
    output out = _oneway binomial;
    weight _wtg / zeros;
    BY &bygroup. &group.;
  RUN;

  data _rowpct (KEEP = &bygroup. &group. _ord statistic stat);
    length statistic $50. stat $50.;
    set _rowpct;
    IF &rsp. EQ 1 THEN DO;
      _ord = 2.1;
      statistic = "Responder";
      END;
    ELSE DO;
      _ord = 2.2;
      statistic = "Non-Responder";
      END;
    stat = TRIM(PUT(COUNT, 10.0-L)) || " (" || TRIM(PUT(PERCENT, BEST4.1)) || "%)";
  run;

  data _oneway (KEEP = &bygroup. &group. _ord statistic stat);
    length statistic $50. stat $50.;
    set _oneway;
    _ord = 1;
    statistic = "n";
    stat = TRIM(PUT(N, 10.0-L));
    output;
    _ord = 2.3;
    statistic = "95% CI for Response Rate (Clopper-Pearson)";
    stat = "(" || TRIM(PUT(XL_BIN * 100, 5.2)) || ", " ||
      TRIM(PUT(XU_BIN * 100, 5.2)) || ")";
    output;
  run;

  data _pdiffci _dtcmh _OR;
    set _NULL_;
    ** stop;
  run;


  %let k = 1;
  %let varl = %scan(&trtgroup., &k., ";");
  %do %while(&varl NE);

    ods output PdiffCLs = _pdiffci&k. (KEEP = &bygroup. RiskDifference Type LowerCL UpperCL)
      CMH = _dtcmh&k. (where = (AltHypothesis EQ "Row Mean Scores Differ"))
      CommonRelRisks = _OR&k. (WHERE = (Statistic EQ: "O")
      KEEP = Statistic &bygroup.  Value LowerCL UpperCL);
    PROC FREQ data = &dtin.  %if &freqfmt. NE %then %do;
      order = formatted
        %end;;
      %if &freqfmt. NE %then %do;
        FORMAT &group. &freqfmt.;
        %end;
      TABLE %if &strata. ne %then %do;
        &strata. *
          %end;
        &group. * &rsp. / cmh2
    riskdiff(COLUMN=1 equal var=null CL=SCORE /* method = wilson */)
    relrisk(equal method = FM CL = FM)
    alpha=0.05
    OUTPCT out = aa;
    weight _wtg / zeros;
    %if &bygroup. ne %then %do;
      BY &bygroup.;
      %end;
    WHERE &group. IN (&refgroup. "&varl.") AND not missing(&rsp.);
    RUN;

    data _pdiffci&k. (KEEP = &bygroup. _ord statistic stat);
      set _pdiffci&k.;
      _ord = 3;
      statistic = "Difference in Response Rates (%) and 95% CI";
      stat = TRIM(PUT(RiskDifference * 100, 5.2-L)) || " (" ||
        TRIM(PUT(LowerCL * 100, 5.2)) || ", " ||
        TRIM(PUT(UpperCL * 100, 5.2)) || ")";
    run;

    data _dtcmh&k.  (KEEP = &bygroup. _ord statistic stat);
      length statistic $50. stat $50.;
      set _dtcmh&k. (DROP = statistic);
      _ord = 3.3;
      statistic = "p-Value (Cochran-Mantel-Haenszel)";
      stat = PUT(Prob, pvalue6.4);
    run;

    data _OR&k. (KEEP = &bygroup. _ord statistic stat);
      length statistic $50. stat $50.;
      set _OR&k. (DROP = statistic WHERE = (not missing(Value)));
      _ord = 4.1;
      statistic = "Odds Ratio (95% CI)";
      stat = TRIM(PUT(Value, 5.2-L)) || " (" ||
        TRIM(PUT(LowerCL, 5.2)) || ", " ||
        TRIM(PUT(UpperCL, 5.2)) || ")";
    run;

    data _pdiffci;
      length &group. $50.;
      SET _pdiffci _pdiffci&k.;
      IF &group. EQ "" THEN &group. = "&varl.";
    run;

    data _dtcmh;
      length &group. $50.;
      SET _dtcmh _dtcmh&k.;
      IF &group. EQ "" THEN &group. = "&varl.";
    run;

    data _OR;
      length &group. $50.;
      SET _OR _OR&k.;
      IF &group. EQ "" THEN &group. = "&varl.";
    run;

    %let k = %eval(&k. + 1);
    %let varl = %scan(&trtgroup., &k., ";");
    %end;


  ods SELECT all;

  data _&dtout.;
    set _rowpct _oneway _pdiffci _dtcmh _OR;
    IF &group. EQ &refgroup. AND missing(stat) THEN stat = "-";
  run;

  proc sql;
    create table _rspstat as
      select DISTINCT
      %if &bygroup. ne %then %do;
      a.&bygroup.,
        %end;
      a.&group., b._ord, b.statistic
        from _&dtout. (KEEP = &bygroup. &group.) as a, _&dtout. (KEEP = _ord statistic) as b
        order BY %if &bygroup. ne %then %do;
        &bygroup.,
          %end;
        &group., _ord;
        quit;
  run;

  proc sort data=_&dtout.;
    by &bygroup. &group. _ord;
  run;

  data _&dtout.;
    merge _&dtout. (IN=a) _rspstat (IN=b);
    by &bygroup. &group. _ord;
    %if &markref. NE %then %do;
      IF b AND not a AND &group. EQ &refgroup. AND stat EQ "" THEN stat = "&markref.";
      %end;
  run;

  proc sort data=_&dtout.;
    by &bygroup. _ord &group. statistic;
  run;

  proc transpose data=_&dtout. out=&dtout. (DROP = _NAME_) delimiter=_;
    %if &transfmt. NE %then %do;
      FORMAT &group. $trt.;
      %end;
    var stat;
    id &group.;
    by &bygroup. _ord statistic;
  run;


  %mend rsp_sum;

%rsp_sum(dtin = ${1:adef}, dtout = ${2:rsptbl}, rsp = ${3:avaln}, group = TRT01P,
  ${4:freqfmt = $trtord., transfmt = $trt., markref = });
